$(function(){
	var userGrid = $("#hightSchoolGrid");
	userGrid.datagrid({
		url: 'hight/list',
		fit:true,
		fitColumns:true,
		rownumbers:true,
		border:false,
		pagination : true,
		columns : [ [ {
			field : 'students',
			title : '学生姓名',
			align: 'center',
			width : 80,
			formatter: function (value, rec) {
		         return rec.students.name;
		     }

		}, {
			field : 'yingJiao',
			title : '应交',
			align: 'center',
			width : 90
		}, {
			field : 'shiJiao',
			title : '实交',
			align: 'center',
			width : 90
		}, {
			field : 'qianJiao',
			title : '欠交',
			align: 'center',
			width : 90
		}, {
			field : 'classes',
			title : '班级',
			align: 'center',
			width : 90,
			formatter: function (value, rec) {
		         return rec.classes.className;
		     }
		}, {
			field : 'ac',
			title : '操作',
			width : 100,
			align : 'center',
			formatter:function(value,row,index){
				var bts = [];
				bts.push('<a data-index="'+index+'" class="ctr edit">编辑</a>');
				bts.push('<a data-id="'+row.id+'" data-index="'+index+'" class="ctr delete">删除</a>');
				return bts.join('');
			}
		}]]/*,
		toolbar:[
		     {
		    	 iconCls:'icon-undo',
		    	 text:"导入",
		    	 url:"",
		    	 handler: function() {
					out();
				}
		     }
		]*/
	});
	
	//详情
	var gridPanel = userGrid.datagrid("getPanel");
	gridPanel.on("click", "a.delete", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要删除此数据吗？", function(r) {
			if(r) {
				$.get("hight/delete/" + id, function(rs) {
					if (rs.success) {
						// 刷新表格
						userGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	}).on("click", "a.edit", function() {
		var index = this.dataset.index;
		
		// 如果只有下标，没有id的情况，要获取id
		var rows = userGrid.datagrid("getRows");
		
		// 同下标获取对应行的数据对象
		var id = rows[index].id;
		hight(id);
	});
	
	var students = $("#student");
	var classes = $("#classes");
	//检索按钮时间
	$("#hightSchoolSearch").on("click",function(){
		userGrid.datagrid("load",{
			students:students.val(),
			classes:classes.val(),
		});
	});
	var form = $("#hightSchoolSearchForm");

	$("#hightSchoolReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		userGrid.datagrid("load", {});
	});
	
	function out(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href: "hight/daoru",
			title: "&nbsp;&nbsp;导出信息",
			height: 570,
			iconCls: 'icon-shoukuan',
			width: 1000,
			modal: true,
			onClose: function() {
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					var hightForm = $("#hightForm");
					if (hightForm.form('validate')) {
						$.post("hight/saveHight", hightForm.serialize(), function(rs) {
							if (rs.success) {
								//刷新数据表格
								userGrid.datagrid("reload");
								//关闭窗口
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}else{
						alert("shibai");
					}
				}
			} ,{
				iconCls : 'icon-cancel',
				text:'取消',
				handler:function(){
				dialog.dialog("close");
				}
			}]

		});
	}
	

	function hight(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href : "hight/edithight"+ (id ? ("/" + id) : ""),
			title : "编辑学费信息",
			iconCls : 'icon-shoukuan',
			height : 300,
			width : 850,
			modal : true,
			onClose : function() {
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					var hightForm = $("#hightForm");
					if (hightForm.form('validate')) {
						$.post("hight/saveHight", hightForm.serialize(), function(rs) {
							if (rs.success) {
								//刷新数据表格
								userGrid.datagrid("reload");
								//关闭窗口
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}else{
						alert("shibai");
					}
				}
			} ,{
				iconCls : 'icon-cancel',
				text:'取消',
				handler:function(){
				dialog.dialog("close");
				}
			}]
			
			
		});
	}
});
	